package com.ptswitch.material.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Hibernate;
import org.springframework.stereotype.Service;

import com.hxzon.project.EDao;
import com.ptswitch.material.dao.BomDao;
import com.ptswitch.material.dao.BomMaterialDao;
import com.ptswitch.material.model.Bom;
import com.ptswitch.material.model.Upload;
import com.ptswitch.material.service.BomService;
import com.ptswitch.material.util.DateUtils;

@Service("bomSer")
public class BomServiceImpl extends LoggerService implements BomService {
	@Resource
	private BomDao bomDao;
	@Resource
	private BomMaterialDao bomMaterialDao;
	@Resource
	private EDao<Upload> uploadDao;

	public long countAll() {
		return bomDao.countAll();
	}
	
	public void delete(String id){
		bomDao.delete(id);
	}

	public void delete(Bom model) {
		if(model!=null){
			bomMaterialDao.deleteAll(model.getBomMaterials());
			bomDao.delete(model);
			uploadDao.delete(model.getUpload());
		}
	}
	
	public void deleteAll(){
		for(Bom bom:bomDao.getAll(-1, -1)){
			bomDao.delete(bom);
			uploadDao.delete(bom.getUpload());
		}
	}
	
	public void deleteAll(List<Bom> models){
		bomDao.deleteAll(models);
	}

	public Bom get(String id) {
		Bom bom=bomDao.get(id);
		if(bom!=null){
			Hibernate.initialize(bom.getBomMaterials());
		}
		return bom;
	}
	
	public Bom get(String code,String version){
		return bomDao.getByCode(code, version);
	}
	
	public List getAll(boolean initialize){
		List<Bom> list=bomDao.getAll();
		if(initialize){
			for(Bom bom:list){
				Hibernate.initialize(bom.getBomMaterials());
			}
		}
		return list;
	}
	
	public List getAll(){
		return bomDao.getAll();
	}
	
	public List getAll(long first,long size){
		return bomDao.getAll(first, size);
	}

	public void save(Bom model) {
		bomDao.save(model);
	}
	
	public void update(Bom oldModel,Bom newModel){
		bomMaterialDao.deleteAll(oldModel.getBomMaterials());
		bomDao.delete(oldModel);
		uploadDao.delete(oldModel.getUpload());
		//add new
		uploadDao.save(newModel.getUpload());
		bomDao.save(newModel);
		bomMaterialDao.saveAll(newModel.getBomMaterials());
	}
	
	public void saveEx(Bom model){
		model.getUpload().setCreateTime(DateUtils.format(new Date()));
		uploadDao.save(model.getUpload());
		bomDao.save(model);
		bomMaterialDao.saveAll(model.getBomMaterials());
	}
	
	public void saveAll(List<Bom> models) {
		bomDao.saveAll(models);
	}

	public long countLike(Bom model) {
		return bomDao.countLike(model);
	}

	public long countLike(String like) {
		return bomDao.countLike(like);
	}

	public List getLike(Bom model, long first, long size) {
		return bomDao.getLike(model, first, size);
	}

	public List getLike(String like, long first, long size) {
		return bomDao.getLike(like, first, size);
	}
	
	public String valid(Bom model){
		String error="";
		if(model.getCode()==null || model.getCode().isEmpty()){
			error+="编码不能为空";
		}
		if(model.getVersion()==null || model.getVersion().isEmpty()){
			error+="版本号不能为空";
		}
		return error;
	}

}
